<template>
	<view class="book-container">
		<u-tabs :list="list" :is-scroll="false" bg-color="" :current="current" @change="change"></u-tabs>
		<view class="pad-16r">
			<view class="book-card" v-for="(item , index) in dataSource" :key="index" @click="checkDetail(index)">
				<view class="book-header">
					<view class="title-container">
						<view class="book-dept">{{ item.name }}</view>
						<view class="title-bg"></view>
					</view>
					<view class="book-status " v-if="item.had">已预约</view>
					<view class="book-status nobook " v-else>未预约</view>
				</view>
				<view class="book-divider"></view>
				<view class="between center">
					<view class="book-row">
						<view class="label">检查费用 ：</view>
						<view class="value">￥{{ item.money }}</view>
					</view>
					<view class="book-btn-row" v-if="!item.had">
						<button class="book-btn" @click.stop="toPage(item)">立即预约</button>
					</view>
				</view>
				<view class="book-row" v-if="item.describe&&mealIndex==index">
					<view class="label">检查描述 ：</view>
					<view class="desc">{{ item.describe }}</view>
				</view>


			</view>
		</view>
	</view>
</template>

<script>
	import {
		medicalTechnologySelect
	} from '../../../../../api/medicalTechnology';
	import {
		medicalTechnologyProjectSelect
	} from '../../../../../api/medicalTechnologyProject';
	import {
		mapGetters
	} from 'vuex'
	export default {
		computed: {
			...mapGetters([
				'userInfo'
			]),

		},
		data() {
			return {
				dataSource: [],
				hadDataSource: [],
				list: [{
						type: 0,
						name: "检验",
						code: "05"
					},
					{
						type: 1,
						name: "检查",
						code: "06"
					}
				],
				current: 0,
				medicaType: "05",
				mealIndex: -1, //套餐索引，用于点击后显示套餐描述 
			}
		},
		onShow() {
			uni.$on('refreshEvent', () => {
				this.init(); // 执行刷新方法
			});
		},
		mounted() {
			this.init();
		},
		methods: {
			init() {
				medicalTechnologySelect({
					patId: this.userInfo.patId,
					status: 1,
					orderBy: 'create_time desc'
				}).then(data => {
					this.hadDataSource = data;
					medicalTechnologyProjectSelect({
						medicalType: this.medicaType
					}).then(res => {
						this.dataSource = res

						if (!this.userInfo) {
							this.$u.toast('请选择就诊卡')
							return;
						}
						this.dataSource.forEach(item => {
							this.hadDataSource.forEach(had => {
								if (item.code == had.code) {
									item.had = true;
								}
							})
						})
					})
				})
			},

			change(e) {
				this.dataSource = []
				this.medicaType = this.list.filter(item => item.type == e)[0].code
				this.current = e
				this.init()
			},
			checkDetail(index) {
				this.mealIndex = index;
			},
			toPage(item) {
				if (!this.userInfo) {
					this.$u.toast('请选择就诊卡')
					return;
				}
				// 这里的id是检验检查的id，并不是预约的id，所以需要设置为空
				item.id = ""; //主要用于修改预约时判断是否有id，如果有，则修改，如果无则增加

				uni.navigateTo({
					url: "/pages/Home/ServePatient/MedicalTechnologyBookPage?item=" + JSON.stringify(item)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.book-card {
		background: #fff;
		border-radius: 16rpx;
		margin: 25rpx 0 0 0;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
		padding: 24rpx 32rpx 18rpx 32rpx;
	}

	.book-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 18rpx;
	}

	.title-container {
		position: relative;
	}

	.book-dept {
		font-size: 32rpx;
		color: #333;
		font-weight: bold;
	}

	.title-bg {
		position: absolute;
		width: 48rpx;
		height: 15rpx;
		background-color: rgba(65, 207, 230, 0.5);
		bottom: 0;
		left: calc(20%);
		z-index: 0
	}

	.book-status {
		background: #4ad49a;
		color: #fff;
		border-radius: 8px;
		padding: 4px 18px;
		right: 8px;
		width: 90px;
		text-align: center;
		white-space: nowrap;
	}

	.book-status.nobook {
		background: #eba242;
	}

	.book-divider {
		height: 1rpx;
		border-top: 1rpx dashed #e0e0e0;
		margin: 18rpx 0 18rpx 0;
		width: 100%;
	}

	.book-row {
		display: flex;
		align-items: center;
		// margin-bottom: 16rpx;
	}

	.label {
		color: #888;
		font-size: 28rpx;
		width: 160rpx;
	}

	.value {
		color: #ff0808;
		font-size: 28rpx;
		flex: 1;
	}

	.desc {
		color: #ca3333;
		font-size: 28rpx;
		flex: 1;
	}

	.nav-btn {
		background: #e0f3ff;
		color: #1ec6e6;
		font-size: 22rpx;
		border-radius: 24rpx;
		padding: 0 18rpx;
		height: 40rpx;
		line-height: 40rpx;
		border: none;
		margin-left: 12rpx;
	}

	.book-btn-row {
		display: flex;
		justify-content: space-between;
	}

	.revise-btn {
		width: 48%;
		height: 64rpx;
		background: #fff;
		color: #1ec6e6;
		font-size: 28rpx;
		border-radius: 44rpx;
		border: 1rpx solid #1ec6e6;
		text-align: center;
		line-height: 64rpx;
	}

	.book-btn {
		width: 150px;
		height: 64rpx;
		background: linear-gradient(90deg, #6ed6f7 0%, #3ec6e7 100%);
		color: #fff;
		font-size: 28rpx;
		border-radius: 44rpx;
		text-align: center;
		line-height: 64rpx;
		border: none;
	}
</style>